<template>
  <view>
    <!-- 轮播图 -->
    <swiper class="swiper-container" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-container">
      <image v-for="(item, i) in navList" :key="i" :src="item.image_src" @click="navClickHandler(item)"></image>
    </view>
    <!-- 楼层数据 -->
    <view class="floor-list">
      
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- floor title -->
        <image class="floor-title" :src="item.floor_title.image_src"></image>
        <!-- floor image container-->
        <view class="floor-image-container">
          <!-- 左边长图 -->
          <navigator class="floor-image-container-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" mode="widthFix" :style="{width: item.product_list[0].image_width +'rpx'}"></image>
          </navigator>
          <!-- 右边短图 -->
          <view class="floor-image-container-right">
            <navigator
              v-for="(item2, i2) in item.product_list"
              v-if="i2 !==0"
              :key="i2"
              :url="item2.url"
            >
              <image
                :src="item2.image_src"
                mode="widthFix"
                :style="{width: item2.image_width +'rpx'}"
              ></image>
            </navigator>
          </view>
        </view>
      </view>
      
    </view>
    
    
  </view>
</template>

<script>
  export default {
    data() {
      return {
        swiperList: [], //首页轮播图
        navList: [], //首页分类
        floorList: [], // 楼层数据
      };
    },
    onLoad() {
      // 发起轮播图数据请求
      this.getSwiperList()
      // 发起请求分类数据
      this.getNavList()
      // 发起请求获取楼层数据
      this.getFloorList()
    },
    methods: {
      async getSwiperList(){
        const { data: res} = await uni.$http.get('/home/swiperdata')
        console.log('res',res)
        if(res.meta.status !== 200) return uni.$showMsg()
        // 把数据赋值到data中
        this.swiperList = res.message
      },
      
      async getNavList(){
        const { data: res } = await uni.$http.get('/home/catitems')
        console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 把数据赋值到data中
        this.navList = res.message
      },
      
      // 跳转到分类tab页面
      navClickHandler(item) {
        console.log(item)
        if(item.name === '分类'){
          uni.switchTab({
            url:'/pages/cate/cate'
          })
        }
      },
      
      // 获取楼层数据
      async getFloorList(){
        const { data: res } = await uni.$http.get('/home/floordata')
        console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 整理数据
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?'+prod.navigator_url.split('?')[1]
          })
        })
        // 把数据赋值到data中
        this.floorList = res.message
      },
    }
  }
</script>

<style lang="scss">
  .swiper-container {
    height: 340rpx;
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }
  .nav-container {
    display: flex;
    justify-content: space-around;
    padding: 32rpx 0rpx;
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor-list {
    .floor-item {
      .floor-title{
        width: 100%;
        height: 60rpx;
      }
      .floor-image-container{
        display: flex;
        margin-left: 8rpx;
        .floor-image-container-right{
          display: flex;
          justify-content: space-around;
          flex-wrap: wrap;
        }
      }
    }
  }
</style>
